const axios = require('axios');

// 添加响应拦截器
axios.interceptors.response.use((response) => {
    // 对响应数据做点什么
    if (response.status === 400 && response.data.errors.length > 0) {
        let message = '';
        response.data.errors.forEach((error) => {
            message += `${error.message}\n`;
        });
        return Promise.reject(message);
    }
    return response;
}, (error) => {
    console.log(error);
    return Promise.reject(error);
});

/*
export default async (url = '', req = {}, type = 'GET') => {
    console.log('--------------------------------------------------', req);
    type = type.toUpperCase();
    if (type == 'GET') {
        let dataStr = ''; // 数据拼接字符串
        Object.keys(req).forEach(key => {
            dataStr += `${key}=${req[key]}&`;
        });
        if (dataStr !== '') {
            dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
            url = `${url}?${dataStr}`;
        }
        return axios.get(url).then(result => result.data);
    }
    console.log('-------------------post', req);
    return axios.post(url, req).then(result => result.data);
};
*/
module.exports = axios;
